<template>
	<div class="diy_edit page_health_consultation" id="health_consultation_edit">
		<div class='warp'>
			<div class='container'>
				<div class='row diy_edit_content_box'>
						<div v-if="$check_field('set','ordinary_user') || $check_field('add','ordinary_user') || $check_field('get','ordinary_user')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								普通用户:
							</span>
						</div>
						<div class="diy_field diy_down">
							<select id="form_ordinary_user" :disabled="disabledObj['ordinary_user_isDisabled']" v-model="form['ordinary_user']" v-if="(form['ordinary_user'] && $check_field('set','ordinary_user')) || (!form['ordinary_user'] && $check_field('add','ordinary_user'))" >
								<option v-for="o in list_user_ordinary_user" :value="o['user_id']">
									{{o['nickname'] + '-' + o['username']}}
								</option>
							</select>
							<span v-else-if="$check_field('get','ordinary_user')">{{ get_user_info("ordinary_user", form['ordinary_user']) }}</span>
						</div>
					</div>
							<div v-if="$check_field('set','expert_user') || $check_field('add','expert_user') || $check_field('get','expert_user')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								专家用户:
							</span>
						</div>
						<div class="diy_field diy_down">
							<select id="form_expert_user" :disabled="disabledObj['expert_user_isDisabled']" v-model="form['expert_user']" v-if="(form['expert_user'] && $check_field('set','expert_user')) || (!form['expert_user'] && $check_field('add','expert_user'))" >
								<option v-for="o in list_user_expert_user" :value="o['user_id']">
									{{o['nickname'] + '-' + o['username']}}
								</option>
							</select>
							<span v-else-if="$check_field('get','expert_user')">{{ get_user_info("expert_user", form['expert_user']) }}</span>
						</div>
					</div>
							<div v-if="$check_field('set','name_of_expert') || $check_field('add','name_of_expert') || $check_field('get','name_of_expert')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								专家姓名:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_name_of_expert" v-model="form['name_of_expert']" placeholder="请输入专家姓名" v-if="(form['name_of_expert'] && $check_field('set','name_of_expert')) || (!form['name_of_expert'] && $check_field('add','name_of_expert'))"  :disabled="disabledObj['name_of_expert_isDisabled']"/>
							<span v-else-if="$check_field('get','name_of_expert')">{{ form['name_of_expert'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','type_of_expert') || $check_field('add','type_of_expert') || $check_field('get','type_of_expert')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								专家类型:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_type_of_expert" v-model="form['type_of_expert']" placeholder="请输入专家类型" v-if="(form['type_of_expert'] && $check_field('set','type_of_expert')) || (!form['type_of_expert'] && $check_field('add','type_of_expert'))"  :disabled="disabledObj['type_of_expert_isDisabled']"/>
							<span v-else-if="$check_field('get','type_of_expert')">{{ form['type_of_expert'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','practitioner_experience') || $check_field('add','practitioner_experience') || $check_field('get','practitioner_experience')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								从业经验:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_practitioner_experience" v-model="form['practitioner_experience']" placeholder="请输入从业经验" v-if="(form['practitioner_experience'] && $check_field('set','practitioner_experience')) || (!form['practitioner_experience'] && $check_field('add','practitioner_experience'))"  :disabled="disabledObj['practitioner_experience_isDisabled']"/>
							<span v-else-if="$check_field('get','practitioner_experience')">{{ form['practitioner_experience'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','areas_of_expertise') || $check_field('add','areas_of_expertise') || $check_field('get','areas_of_expertise')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								擅长领域:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_areas_of_expertise" v-model="form['areas_of_expertise']" placeholder="请输入擅长领域" v-if="(form['areas_of_expertise'] && $check_field('set','areas_of_expertise')) || (!form['areas_of_expertise'] && $check_field('add','areas_of_expertise'))"  :disabled="disabledObj['areas_of_expertise_isDisabled']"/>
							<span v-else-if="$check_field('get','areas_of_expertise')">{{ form['areas_of_expertise'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','consulting_price') || $check_field('add','consulting_price') || $check_field('get','consulting_price')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								咨询价格:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_consulting_price" v-model="form['consulting_price']" placeholder="请输入咨询价格" v-if="(form['consulting_price'] && $check_field('set','consulting_price')) || (!form['consulting_price'] && $check_field('add','consulting_price'))"  :disabled="disabledObj['consulting_price_isDisabled']"/>
							<span v-else-if="$check_field('get','consulting_price')">{{ form['consulting_price'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','tongue_image_upload') || $check_field('add','tongue_image_upload') || $check_field('get','tongue_image_upload')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								舌像上传:
							</span>
						</div>
								<!-- 图片 -->
						<input type="file" :disabled="disabledObj['tongue_image_upload_isDisabled']" style="display: none;" id="form_img_tongue_image_upload" title="tongue_image_upload" @change="change_file($event.target.files,'tongue_image_upload')"/>
						<!-- 修改权限 -->
						<div class="diy_field diy_img" v-if="form['tongue_image_upload'] && $check_field('set','tongue_image_upload')">
							<label for="form_img_tongue_image_upload">
								<img :src="$fullUrl(form['tongue_image_upload'])" />
							</label>
						</div>
						<!-- 添加权限 -->
						<div class="diy_field diy_img" v-else-if="!form['tongue_image_upload'] && $check_field('add','tongue_image_upload')">
							<label for="form_img_tongue_image_upload">
								<div class="btn_add_img">
									<span>+</span>
								</div>
							</label>
						</div>
						<!-- 查询权限 -->
						<div class="diy_field diy_img" v-else-if="$check_field('get','tongue_image_upload')">
							<img :src="$fullUrl(form['tongue_image_upload'])" />
						</div>
							</div>
							<div v-if="$check_field('set','date_of_consultation') || $check_field('add','date_of_consultation') || $check_field('get','date_of_consultation')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								咨询日期:
							</span>
						</div>
								<!-- 日期 -->
						<div class="diy_field diy_date">
							<input type="date" :disabled="disabledObj['date_of_consultation_isDisabled']" id="form_date_of_consultation" v-model="form['date_of_consultation']" placeholder="请输入咨询日期" v-if="(form['date_of_consultation'] && $check_field('set','date_of_consultation')) || (!form['date_of_consultation'] && $check_field('add','date_of_consultation'))"/>
							<span v-else-if="$check_field('get','date_of_consultation')">{{ form['date_of_consultation'] }}</span>
						</div>
							</div>
							<div v-if="$check_field('set','consulting_content') || $check_field('add','consulting_content') || $check_field('get','consulting_content')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								咨询内容:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_consulting_content" v-model="form['consulting_content']" placeholder="请输入咨询内容" v-if="(form['consulting_content'] && $check_field('set','consulting_content')) || (!form['consulting_content'] && $check_field('add','consulting_content'))"  :disabled="disabledObj['consulting_content_isDisabled']"/>
							<span v-else-if="$check_field('get','consulting_content')">{{ form['consulting_content'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','details_of_the_consultation') || $check_field('add','details_of_the_consultation') || $check_field('get','details_of_the_consultation')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								咨询详情:
							</span>
						</div>
								<!-- 多文本 -->
						<div class="diy_field diy_desc">
							<textarea id="form_details_of_the_consultation" v-model="form['details_of_the_consultation']" v-if="(form['details_of_the_consultation'] && $check_field('set','details_of_the_consultation')) || (!form['details_of_the_consultation'] && $check_field('add','details_of_the_consultation'))" :disabled="disabledObj['details_of_the_consultation_isDisabled']" />
							<span v-else-if="$check_field('get','details_of_the_consultation')">{{ form['details_of_the_consultation'] }}</span>
						</div>
							</div>
	




				</div>
				<div class="diy_edit_submit_box row">
					<div class="col-12">
						<div class="btn_box">
							<button class="btn_submit" @click="submit()">提交</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import mixin from "@/mixins/page.js";
	export default {
		mixins: [mixin],
		components: {},
		data() {
			return {
				url_get_obj: "~/api/health_consultation/get_obj?",
				url_add: "~/api/health_consultation/add?",
				url_set: "~/api/health_consultation/set?",

				// 登录权限
				oauth: {
					"signIn": true,
					"user_group": []
				},

				// 查询条件
				query: {
						"ordinary_user": 0,
							"expert_user": 0,
							"name_of_expert": "",
							"type_of_expert": "",
							"practitioner_experience": "",
							"areas_of_expertise": "",
							"consulting_price": "",
							"tongue_image_upload": "",
							"date_of_consultation": "",
							"consulting_content": "",
							"details_of_the_consultation": "",
						"health_consultation_id": 0,
				},

				obj: {
						"ordinary_user": 0, // 普通用户
							"expert_user": 0, // 专家用户
							"name_of_expert":  '', // 专家姓名
							"type_of_expert":  '', // 专家类型
							"practitioner_experience":  '', // 从业经验
							"areas_of_expertise":  '', // 擅长领域
							"consulting_price":  '', // 咨询价格
							"tongue_image_upload":  '', // 舌像上传
							"date_of_consultation": new Date().getTime(),
							"consulting_content":  '', // 咨询内容
							"details_of_the_consultation":  '', // 咨询详情
						"health_consultation_id": 0,
				},

				// 表单字段
				form: {
						"ordinary_user": 0, // 普通用户
							"expert_user": 0, // 专家用户
							"name_of_expert":  '', // 专家姓名
							"type_of_expert":  '', // 专家类型
							"practitioner_experience":  '', // 从业经验
							"areas_of_expertise":  '', // 擅长领域
							"consulting_price":  '', // 咨询价格
							"tongue_image_upload":  '', // 舌像上传
							"date_of_consultation": new Date().getTime(),
							"consulting_content":  '', // 咨询内容
							"details_of_the_consultation":  '', // 咨询详情
						"health_consultation_id": 0,
				},
				disabledObj:{
						"ordinary_user_isDisabled": false,
							"expert_user_isDisabled": false,
							"name_of_expert_isDisabled": false,
							"type_of_expert_isDisabled": false,
							"practitioner_experience_isDisabled": false,
							"areas_of_expertise_isDisabled": false,
							"consulting_price_isDisabled": false,
							"tongue_image_upload_isDisabled": false,
							"date_of_consultation_isDisabled": false,
							"consulting_content_isDisabled": false,
							"details_of_the_consultation_isDisabled": false,
					},

						// 用户列表
				list_user_ordinary_user: [],
								// 用户列表
				list_user_expert_user: [],
																													
				// ID字段
				field: "health_consultation_id",

			}
		},
		methods: {
																																																																																														formatDateValue(value) {
		  const date = new Date(value);
		  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
		},
		async submit(param, func){
			if (!param) {
				param = this.form;
			}
						var pm = this.events("submit_before", Object.assign({}, param)) || param;
			var msg = await this.events("submit_check", pm);
			var ret;
			if (msg) {
				this.$toast(msg, 'danger');
			} else {
																																						ret = this.events("submit_main", pm, func);
			}
			return ret;
		},
		
      /**
       * 提交前验证事件
       * @param {Object} 请求参数
       * @return {String} 验证成功返回null, 失败返回错误提示
       */
            submit_check(param) {
      																																											if (!param.date_of_consultation){
			return "咨询日期不能为空";
		}
															        return null;
      },
					/**
			 * 获取普通用户用户列表
			 */
			async get_list_user_ordinary_user() {
				var json = await this.$get("~/api/user/get_list?user_group=普通用户");
				if(json.result && json.result.list){
					this.list_user_ordinary_user = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
					async get_user_session_ordinary_user(){
				var _this = this;
				var json = await this.$get("~/api/user_group/get_obj?name=普通用户");
				if(json.result && json.result.obj){
					var source_table = json.result.obj.source_table;
					var user_id = _this.$store.state.user.user_id;
					if (user_id){
						var url = "~/api/"+source_table+"/get_obj?"
						this.$get(url, {"user_id":_this.$store.state.user.user_id}, function(res) {
							if (res.result && res.result.obj) {
								var arr = []
								for (let key in res.result.obj) {
									arr.push(key)
								}
								var arrForm = []
								for (let key in _this.form) {
									arrForm.push(key)
								}
								_this.form["ordinary_user"] = user_id
								_this.disabledObj['ordinary_user' + '_isDisabled'] = true
								for (var i=0;i<arr.length;i++){
                  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
                    for (var j = 0; j < arrForm.length; j++) {
                      if (arr[i] === arrForm[j]) {
                        if (arr[i] !== "ordinary_user") {
                          _this.form[arrForm[j]] = res.result.obj[arr[i]]
                          _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                          break;
                        }
                      }
                    }
                  }
								}
							}
						});
					}
				}
				else if(json.error){
					console.error(json.error);
				}
			},
	
						/**
			 * 获取专家用户用户列表
			 */
			async get_list_user_expert_user() {
				var json = await this.$get("~/api/user/get_list?user_group=专家用户");
				if(json.result && json.result.list){
					this.list_user_expert_user = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
		
				
				
				
				
				
				
				
				
				
		  		get_user_info(name,id){
				var obj = null;
  				  if (name == 'ordinary_user'){
					  obj = this.list_user_ordinary_user.getObj({"user_id":id});
				  }
    				  if (name == 'expert_user'){
					  obj = this.list_user_expert_user.getObj({"user_id":id});
				  }
                    				var ret = "";
				if(obj){
				  ret = obj.nickname+"-"+obj.username;
				}
				return ret;
			},
			/**
			 * 修改文件
			 * @param { Object } files 上传文件对象
			 * @param { String } str 表单的属性名
			 */
			change_file(files, str) {
				var form = new FormData();
				form.append("file", files[0]);
				this.$post("~/api/health_consultation/upload?", form, (res) => {
					if (res.result) {
						this.form[str] = res.result.url;
					} else if (res.error) {
						this.$toast(res.error.message);
					}
				});
			},
			
			/**
			 * 修改文件
			 * @param { Object } files 上传文件对象
			 * @param { String } str 表单的属性名
			 */
			change_file_multiple(files, str) {
				let _this = this;
				var form = new FormData();
				for (var i = 0; i < files.length; i++) {
					form.set("file", files[i]);
					_this.$post("~/api/health_consultation/upload?", form, (res) => {
						if (res.result) {
							if (_this.form[str].length > 0) {
								_this.form[str].push(res.result.url);
							} else {
								_this.form[str] = [res.result.url];
							}
						} else if (res.error) {
							_this.$toast(res.error.message);
						}
					});
				}
			},

			/**
			 * 获取对象后获取缓存表单
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_before(param){
				var form = $.db.get("form");
				// if (form) {
        //   delete(form.examine_state)
        //   delete(form.examine_reply)
        //   this.obj = $.push(this.obj ,form);
				// 	this.form = $.push(this.form ,form);
				// }
				// var arr = []
				// for (let key in form) {
				// 	arr.push(key)
				// }
				// for (var i=0;i<arr.length;i++){
				// 	this.disabledObj[arr[i] + '_isDisabled'] = true
				// }
        if (form) {
          var arr = []
          for (let key in form) {
            arr.push(key)
          }
          var arrForm = []
          for (let key in this.form) {
            arrForm.push(key)
          }
          for (var i=0;i<arr.length;i++){
            if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
              for (var j = 0; j < arrForm.length; j++) {
                if (arrForm[j] === arr[i]) {
                  this.form[arrForm[j]] = form[arr[i]]
                  this.obj[arrForm[j]] = form[arr[i]]
                  this.disabledObj[arrForm[j] + '_isDisabled'] = true
                  break;
                }
              }
			  if(arr[i] === "source_table"){
			  	this.form['source_table'] = form[arr[i]]
			  }
			  if(arr[i] === "source_id"){
			  	this.form['source_id'] = form[arr[i]]
			  }
			  if(arr[i] === "source_user_id"){
			  	this.form['source_user_id'] = form[arr[i]]
			  }
            }
          }
        }
																										        if (this.form["date_of_consultation"] && JSON.stringify(this.form["date_of_consultation"]).indexOf("-")===-1) {
          this.form["date_of_consultation"] = this.$toTime(parseInt(this.form["date_of_consultation"]), "yyyy-MM-dd")
        }
								
        $.db.del("form");
				return param;
			},

			/**
			 * 获取对象后获取缓存表单
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json ,func){
				// var form = $.db.get("form");
				// var obj = Object.assign({} ,form ,this.obj);
				// if (obj) {
        //   delete(obj.examine_state)
        //   delete(obj.examine_reply)
				// 	this.obj = $.push(this.obj ,obj);
				// }
				// if (form) {
        //   delete(form.examine_state)
        //   delete(form.examine_reply)
				// 	this.form = $.push(this.form ,form);
				// }

				if(func){
					func(json);
				}
			},

		},
		created() {
					this.get_user_session_ordinary_user();
					this.get_list_user_ordinary_user();
							this.get_list_user_expert_user();
																														},
	}
</script>

<style>
	.diy_compute{
		line-height: 40px;
	}
	.diy_field.diy_img_multiple{
		margin: 0;
	}
	.diy_field.diy_img_multiple div{
		float: left;
		position: relative;
		margin: 0 10px 10px 0;
	}
	.diy_field.diy_img_multiple img{
		height: 100px;
		width: auto;
	}
	.diy_field.diy_img_multiple span{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}



</style>
